package com.javasm.adminapi.Tspu.contorller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageInfo;
import com.javasm.adminapi.common.R;
import com.javasm.adminapi.module.product.QueryVo;
import com.javasm.adminapi.module.product.entity.TSpu;
import com.javasm.adminapi.module.product.service.TSpuService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.management.Query;
import java.io.IOException;
import java.util.List;

/**
 * @className: TSpuController
 * @description:
 * @author: cx
 * @date: 2025/10/14 22:32
 * @version: 0.1
 * @since: jdk17
 */
@RestController
@RequestMapping("/tSpu")
public class TSpuController {
    @Resource
    TSpuService tSpuService;
    //获取tspu数据
    @GetMapping("/Tspage")
    public R page(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                  @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        // 设置默认值
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }
        PageInfo<TSpu> pageInfo = tSpuService.listByPage(pageNum, pageSize);
        return R.ok(pageInfo);
    }

    @GetMapping("/info")
    public R info(QueryVo vo){
        PageInfo<TSpu>  list = tSpuService.queryByInfo(vo);
        return R.ok(list);
    }

    @PostMapping("/add")
    public R add(@RequestBody TSpu tSpu){
        return R.ok(tSpuService.addTSput(tSpu));
    }

    //修改接口
    @PutMapping("/update")
    public R update(@RequestBody TSpu tSpu){
        tSpuService.updateByIdinfo(tSpu);
        return R.ok();
    }

    //批量删除接口
    @PostMapping("/delete")
    public R delete(@RequestBody List<Integer> ids){
        tSpuService.removeByIds(ids);
        return R.ok();
    }

    @PostMapping("/import/excel")
    public R importExcel(MultipartFile file) throws IOException {
        //导入数据 存储数据库
        EasyExcel.read(file.getInputStream(),
                TSpu.class,
                new PageReadListener<TSpu>(list->{
                    tSpuService.saveBatch(list);
                })).sheet().doRead();
        return R.ok();
    }




}
